package test.poi.xssf;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 功能：
 * 作者： yangyan
 * 时间： 2015/4/13 .
 */
public class PoiExpExcel {


    public static void main(String[] args) {

        //        表头
        String headers[] = new String[]{"user", "gender", "age"};
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = hssfWorkbook.createSheet("sheet1");

        XSSFRow row = sheet.createRow(0);

        XSSFCell cell = null;

//        插入表头
        for (int i = 0; i < headers.length; i++) {

            cell = row.createCell(i);
            cell.setCellValue(headers[i]);
        }

//        追加数据

        for (int i = 1; i < 10; i++) {

            XSSFRow row1 = sheet.createRow(i);
            XSSFCell cell1 = row1.createCell(0);

            cell1.setCellValue("a" + i);

            cell1 = row1.createCell(1);

            cell1.setCellValue("男");

            cell1 = row1.createCell(2);

            cell1.setCellValue(RandomStringUtils.randomNumeric(2));

        }


        File file = new File("test_file/poi_test.xlsx");
        file.getParentFile().mkdirs();
        try {
            file.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            hssfWorkbook.write(fileOutputStream);
            hssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
